<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('院内医废预警量配置列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse none">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <p>医院id：</p>
                                <input type="text" name="hospitalId"/>
                            </li>
                            <li>
                                <p>医废类型：</p>
                                <select name="type" th:with="type=${@dict.getType('yf_trash_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <p>最大库存量：</p>
                                <input type="text" name="maxStore"/>
                            </li>
                            <li>
                                <p>创建者：</p>
                                <input type="text" name="createId"/>
                            </li>
                            <li class="select-time">
                                <p>创建时间：</p>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginCreateTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endCreateTime]"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <!--<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="yf:warning:add">-->
                    <!--<i class="fa fa-plus"></i> 添加-->
                <!--</a>-->
                <!--<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="yf:warning:edit">-->
                    <!--<i class="fa fa-edit"></i> 修改-->
                <!--</a>-->
                <!--<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="yf:warning:remove">-->
                    <!--<i class="fa fa-remove"></i> 删除-->
                <!--</a>-->
                <a class="btn btn-success" onclick="saveYsStorangeWarning()" shiro:hasPermission="yf:warning:add">
                    <i class="fa fa-plus"></i> 保存
                </a>
                <!--  医院暂存处  -->
                <span style="margin-left:30px;display:inline-block;">医院暂存点：</span>
                <span style="width:350px;display:inline-block;">
                    <select id="staging" name="staging" class="form-control"   >
                        <!--<option th:each="arrayS:${database}" th:text="${arrayS}" th:value="${arrayS}"></option>-->
                    </select>
                </span>

                <!--<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="yf:warning:export">-->
                    <!--<i class="fa fa-download"></i> 导出-->
                 <!--</a>-->
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
                <div style="margin-left: 180px;color:#FF0000;font-weight:bold;font-size:18px">
                    注意： 预警总值 = 医疗机构医废的平均日产量(kg) * 2 </div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('yf:warning:edit')}]];
        var removeFlag = [[${@permission.hasPermi('yf:warning:remove')}]];
        var typeDatas = [[${@dict.getType('yf_trash_type')}]];
        var prefix = ctx + "yf/warning";
        var options;

        //修改为对象
        var hostid = sessionStorage.getItem("hospital_ids");    // 医院id
        var stagingId = 0;

        // 刷新按钮
        $('#btnRefresh').click(function () {
            tableRefresh();
        })

        $(function() {
            options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                saveYfTypeWarnUrl: prefix + "/saveYfTypeWarn",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "院内医废预警量配置",
                showSearch: false,  // 不显示搜索
                showToggle: false,  // 不显示切换
                rowStyle: rowStyle,//自定义行样式
                queryParams: {
                    // hospitalId: hostid
                    hospitalId: stagingId
                },
                columns: [
                // {
                //     checkbox: false,
                //     width: '15%'
                // },
                {
                    field : 'id',
                    title : 'id',
                    width: '15%',
                    visible: false
                },
                {
                    field : 'hospitalId',
                    title : '医院id',
                    width: '15%',
                    visible: false
                },
                {
                    field : 'yfTypeValue',
                    title : '医废类型value',
                    width: '15%',
                    visible: false
                },
                {
                    field : 'yfTypeLabel',
                    title : '医废类型',
                    align: 'center',
                    width: '25%'
                    // formatter: function(value, row, index) {
                    //    return $.table.selectDictLabel(typeDatas, value);
                    // }
                },
                {
                    field : 'maxStore',
                    title : '最大暂存量(kg)',
                    width: '15%',
                    editable : {
                        type : 'text' // 数据显示在文本框内
                    },
                    formatter : function(value, row, index) {       // oninput="if(value.length>5)value=value.slice(0,5)"
                        return "<input type='number' value= '" + (row.maxStore!=null?row.maxStore:'')
                            + "' id='maxStore' name='maxStore' style='border-style:none;width:100px;' maxlength='5' "
                            + " oninput='totalsum("+ index + ",this)' >";
                    }
                },
                // {
                //     field : 'createId',
                //     title : '创建者'
                // },
                {
                    field : 'createTime', 
                    title : '创建时间',
                    width: '20%'
                },
                {
                    field : 'remark', 
                    title : '备注'
                }
                // ,
                // {
                //     title: '操作',
                //     align: 'center',
                //     formatter: function(value, row, index) {
                //         var actions = [];
                //         actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                //         actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                //         return actions.join('');
                //     }
                // }
                ]
            };

            // $.table.init(options);


            // 查询医院暂存点
            initStagings();

        });

        // 修改行标记删除的颜色
        function rowStyle(row, index) {
            if (row.yfTypeValue == 'maxstore') {
                return {
                    classes: 'blackbold'
                }
            }
            return {};
        }

        // 刷新表格
        function tableRefresh(){
            $.table.search(null, null, {
                // hospitalId: hostid
                hospitalId: stagingId
            });
        }


        //获取医院暂存点
        function initStagings() {
            var getStagingsUrl = prefix + "/getStangsByHospitalId";
            // $("#province").empty().append('<option value=0>---- 选择省 ----</option>');
            $.post(getStagingsUrl, {hospitalId: hostid}, function (data) {
                if( data.rows != "" && data.rows != null ){     // 如果医院有暂存点
                    $.each(data.rows, function (i, d) {
                        // alert( data.rows.length );
                        $("#staging").append("<option value='" + d.deptId + "'>" + d.deptName + "</option>");

                        if( i == 0 ){
                            options.queryParams.hospitalId = d.deptId;
                        }

                    });

                    // 查询暂存点预警值
                    $.table.init(options);
                }

            }, 'json');


            // 暂存点切换
            $("select[name=staging]").on('change', function () {

                stagingId = $(this).val();

                // 刷新表格
                tableRefresh();

            })


        }


        // 计算预警总值
        function totalsum(index,max){
            // alert(index);
            if(max.value.length>5){
                max.value=max.value.slice(0,5);
            };
            // 输入框
            var warns = [];
            $("input[id='maxStore']").each(function(){
                warns.push($(this).val());
            });
            // 如果修改的是预警总值则不计算
            if( index != warns.length-1 ){
                var warnsum = 0;
                for(var i=0;i< warns.length-1;i++){
                    if( warns[i] != null && warns[i] != '' ){
                        warnsum = warnsum + parseInt(warns[i]) ;
                    }
                }
                // 给预警总值赋值
                $("input[id='maxStore']")[warns.length-1].value = warnsum;
            }
        }

        // 保存医废预警量
        function saveYsStorangeWarning(){
            // 表格
            var data = $('#bootstrap-table').bootstrapTable('getData');
            // 输入框
            var warns = [];
            $("input[id='maxStore']").each(function(){
                warns.push($(this).val());
            });
            // 拼接json
            var str;
            str = '[';
            var warnsum = 0;
            var maxstore = 0;
            for(var i=0;i< data.length;i++){
                str = str + '{' + '"id":' + data[i].id ;
                str = str + ',' + '"hospitalId":' + data[i].hospitalId;
                str = str + ',' + '"yfTypeValue":' + '"' + data[i].yfTypeValue + '"';
                str = str + ',' + '"maxStore":'+ (warns[i]==''?null:warns[i]) + '}';
                // 如果不是最后一行
                if( i != data.length - 1  ){
                    str = str + ',';
                    if( warns[i] != null && warns[i] != '' ){
                        warnsum = warnsum + parseInt(warns[i]) ;
                    }
                }else{  // 最后一行
                    if( warns[i] == null || warns[i] == 0 ){
                        $.modal.alertWarning("预警总值不能为空或0");
                        return;
                    }else{
                        maxstore = warns[i];
                    }
                }
            }

            // alert( maxstore );
            // alert( warnsum );

            if( parseInt(maxstore) < parseInt(warnsum) ){
                $.modal.alertWarning("预警总值不能小于各类型医废预警值和");
                return;
            }

            // $.modal.alertWarning("正确");
            // return;

            str = str + ']';
            // alert( str );
            $.modal.confirm("确认要保存医废预警量吗?", function () {
                var url = table.options.saveYfTypeWarnUrl;
                // $.operate.submit(url, "post", "json", str );
                $.modal.loading("正在处理中，请稍后...");
                $.ajax({
                    type : "post",
                    url : url,
                    data : str,
                    contentType:"application/json",
                    dataType : "json",
                    success : function(data) {
                        $.modal.closeLoading();
                        if( data == 0 ){
                            $.modal.alertWarning("操作失败");
                        }else if( data == 1 ){

                            stagingId = $("select[name=staging]").val();

                            // 刷新
                            tableRefresh();
                        }
                    }
                });

            });

        }


    </script>
</body>
</html>